// pages/calendar/personalDetail/p.js
import { getScheduleInfo, personalInformation } from '../../../api/calendar';
import { getToken } from '../../../utils/storage';
import { timeoutTask } from '../../../utils/index';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //编辑照片
    showclosebtn: false,
    //popup
    show: false,
    //轮播图显示
    swiperPages: 0,
    //档期资料
    scheduleDate: '',
    //用户头像信息
    useravator: '',
    //上午颜色
    amColor: '#D3D3D3',
    lunchColor: '#D3D3D3',
    pmColor: '#D3D3D3',
    dinnerColor: '#D3D3D3',
    nightColor: '#D3D3D3',
    worksVideo: '',
    editPopupVisible: false,
    editPopupAni: false,
    uploadCover: false,
  },
  hideEditPopup: function() {
    this.setData({
      editPopupAni: false,
    });
    timeoutTask(() => {
      this.setData({
        editPopupVisible: false,
      });
    }, 300);
  },
  showEditPopup: function() {
    this.setData({
      editPopupVisible: true,
    });
    timeoutTask(() => {
      this.setData({
        editPopupAni: true,
      });
    }, 100);
  },
  //去修改资料
  gotoEditPersonal() {
    wx.navigateTo({
      url: '/pages/calendar/editPersonalDetail/editPersonalDetail',
    });
    this.onClosepopup();
    this.hideEditPopup();
  },
  //上传视频
  chooseVideo() {
    let that = this;

    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: 'back',
      // compressed: false,
      success: function(res) {
        that.setData({
          uploadCover: true,
        });

        let filepath = res.tempFilePath;
        const tmpToken = getToken();
        that.hideEditPopup();
        wx.uploadFile({
          filePath: filepath,
          name: 'file',
          url: 'https://www.hudongmiao.com/beiJing/upload',
          header: {
            token: tmpToken,
            'Content-Type': 'multipart/form-data',
          },

          success(res) {
            //
            let webidpic = JSON.parse(res.data);
            let newstr;
            if (that.data.worksVideo == '') {
              newstr = webidpic.data.filePath;
            } else {
              newstr = that.data.worksVideo + ',' + webidpic.data.filePath;
            }

            personalInformation({
              avator: that.data.useravator.avator,
              emcee_name: that.data.useravator.emcee_name,
              common_address: that.data.useravator.common_address,
              sex: that.data.useravator.sex,
              user_describe: that.data.useravator.user_describe,
              works: newstr,
            }).then(res => {
              // setTimeout(()=>{
              //   wx.showToast({
              //     title: '上传成功',
              //   })
              // },100);
              // wx.hideLoading({
              //   success: (res) => {
              //
              //   },
              // })
              that.setData({
                uploadCover: false,
              });
              setTimeout(() => {
                wx.showToast({
                  title: '上传成功',
                });
              }, 1000);
              that.onShow();
            });
          },
          fail: function(res) {},
        });
      },
      fail: function(res) {},
    });

    this.onClosepopup();
  },
  //去添加档期
  gotoEditOrAdd() {
    wx.navigateTo({
      url: '/pages/calendar/createSchedule/createSchedule',
    });
    this.onClosepopup();
    this.hideEditPopup();
  },
  //开启弹出层
  onShowpopup() {
    this.setData({
      editPopupVisible: true,
    });
  },
  //关闭弹出层
  onClosepopup(e) {
    this.setData({
      editPopupVisible: false,
    });
  },
  //滑动页面时
  swiperchange(e) {
    //
    this.setData({
      swiperPages: e.detail.current,
    });
  },
  //当前页面
  datapage: function(e) {
    this.setData({
      swiperPages: e.target.dataset.id,
    });
    // ;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // getScheduleInfo({
    //   'id':''
    // }).then((res)=>{
    //
    //   this.setData({
    //     scheduleDate:res.data.list,
    //     useravator: res.data.user.avator
    //   })
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    getScheduleInfo({
      id: '',
    }).then(res => {
      for (let i = 0; i < res.data.list.length; i++) {
        for (let j = 0; j < res.data.list[i].dataList.length; j++) {
          res.data.list[i].dataList[j].dateReal =
            res.data.list[i].dataList[j].date;
          res.data.list[i].dataList[j].date = res.data.list[i].dataList[
            j
          ].date.substring(8);
          let newinterval = res.data.list[i].dataList[j].time_interval
            .split(',')
            .join(' ');
          res.data.list[i].dataList[j].time_interval = newinterval;
          if (newinterval.indexOf('上午') != -1) {
            res.data.list[i].dataList[j].amColor = '#ef7995';
            res.data.list[i].dataList[j].lunchColor = '#D3D3D3';
            res.data.list[i].dataList[j].pmColor = '#D3D3D3';
            res.data.list[i].dataList[j].dinnerColor = '#D3D3D3';
            res.data.list[i].dataList[j].nightColor = '#D3D3D3';
          }
          if (newinterval.indexOf('午宴') != -1) {
            res.data.list[i].dataList[j].amColor = '#D3D3D3';
            res.data.list[i].dataList[j].lunchColor = '#ef7995';
            res.data.list[i].dataList[j].pmColor = '#D3D3D3';
            res.data.list[i].dataList[j].dinnerColor = '#D3D3D3';
            res.data.list[i].dataList[j].nightColor = '#D3D3D3';
          }
          if (newinterval.indexOf('下午') != -1) {
            res.data.list[i].dataList[j].amColor = '#D3D3D3';
            res.data.list[i].dataList[j].lunchColor = '#D3D3D3';
            res.data.list[i].dataList[j].pmColor = '#ef7995';
            res.data.list[i].dataList[j].dinnerColor = '#D3D3D3';
            res.data.list[i].dataList[j].nightColor = '#D3D3D3';
          }
          if (newinterval.indexOf('晚宴') != -1) {
            res.data.list[i].dataList[j].amColor = '#D3D3D3';
            res.data.list[i].dataList[j].lunchColor = '#D3D3D3';
            res.data.list[i].dataList[j].pmColor = '#D3D3D3';
            res.data.list[i].dataList[j].dinnerColor = '#ef7995';
            res.data.list[i].dataList[j].nightColor = '#D3D3D3';
          }
          if (newinterval.indexOf('晚上') != -1) {
            res.data.list[i].dataList[j].amColor = '#D3D3D3';
            res.data.list[i].dataList[j].lunchColor = '#D3D3D3';
            res.data.list[i].dataList[j].pmColor = '#D3D3D3';
            res.data.list[i].dataList[j].dinnerColor = '#D3D3D3';
            res.data.list[i].dataList[j].nightColor = '#ef7995';
          }
        }
      }
      if (res.data.user.works == undefined || res.data.user.works == '') {
        this.setData({
          scheduleDate: res.data.list,
          useravator: res.data.user,
          noworksVideo: true,
          showclosebtn: false,
        });
      } else {
        this.setData({
          scheduleDate: res.data.list,
          useravator: res.data.user,
          worksVideo: res.data.user.works.split(','), //提醒改
          noworksVideo: false,
        });
      }
    });
  },
  //完成编辑
  editVideoDone() {
    this.setData({
      showclosebtn: false,
    });
  },
  //编辑视频
  editVideo() {
    this.setData({
      showclosebtn: true,
    });
  },
  //删除视频
  deltVideo(e) {
    let that = this;
    wx.showModal({
      cancelColor: '#FF0000',
      title: '提示',
      content: '确定要删除视频吗？',
      success(res) {
        if (res.confirm) {
          let deltItem = e.target.id;
          let newarr = that.data.worksVideo;
          // newarr.splice();
          newarr.splice(deltItem, 1);
          // ;
          personalInformation({
            avator: that.data.useravator.avator,
            emcee_name: that.data.useravator.emcee_name,
            common_address: that.data.useravator.common_address,
            sex: that.data.useravator.sex,
            user_describe: that.data.useravator.user_describe,
            works: newarr,
          }).then(res => {
            that.onShow();
          });
        }
      },
    });
  },
  //返回上一层
  gobackmain() {
    wx.navigateBack({
      delta: 1,
    });
  },
  gotoedit(e) {
    if (!e.currentTarget.dataset.active) {
      return;
    }
    const tmpDate = this.data.scheduleDate[e.currentTarget.dataset.index];
    const tmpDay = tmpDate.dataList.find(
      item => item.schedule_id === e.currentTarget.dataset.id
    );
    ;
    wx.navigateTo({
      url: '/pages/calendar/editSchedule/editSchdule',
      events: {
        editDate: function(data) {},
      },
      success: function(res) {
        res.eventChannel.emit('editDate', tmpDay);
      },
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {},
});
